package com.zyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyb.mapper.TeacherMapper;
import com.zyb.pojo.LoginForm;
import com.zyb.pojo.Teacher;
import com.zyb.service.TeacherService;
import com.zyb.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    //登录校验
    @Override
    public Teacher login(LoginForm loginForm) {
        QueryWrapper<Teacher> wrapper=new QueryWrapper<>();
        wrapper.eq("name",loginForm.getUsername());
        wrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        Teacher teacher = baseMapper.selectOne(wrapper);
        return teacher;
    }

    //获取登录人的信息并查询
    @Override
    public Teacher getAdminById(Long userId) {
        QueryWrapper<Teacher> wrapper=new QueryWrapper<>();
        wrapper.eq("id",userId);
        return baseMapper.selectOne(wrapper);
    }

    //条件查询带分页
    @Override
    public IPage<Teacher> getTeacherPage(Integer pageNo, Integer pageSize, Teacher teacher) {
        IPage<Teacher> teacherIPage=new Page<>(pageNo,pageSize);
        String name = teacher.getName();
        String clazzName = teacher.getClazzName();
        QueryWrapper<Teacher> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(clazzName)){
            wrapper.eq("clazz_name",clazzName);
        }
        wrapper.orderByDesc("id");
        IPage<Teacher> iPage = baseMapper.selectPage(teacherIPage, wrapper);
        return iPage;
    }
}
